package com.sne.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sne.common.constant.RedisConstant;
import com.sne.common.exception.CustomException;
import com.sne.sys.entity.SysDict;
import com.sne.sys.entity.SysDictItem;
import com.sne.sys.mapper.SysDictItemMapper;
import com.sne.sys.service.ISysDictItemService;
import com.sne.sys.service.ISysDictService;
import com.sne.sys.vo.SysDictItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.interceptor.CacheEvictOperation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典项 服务实现类
 * </p>
 *
 * @author fanghh
 * @since 2020-01-10
 */
@Slf4j
@Service
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements ISysDictItemService {

    @Autowired
    private ISysDictService iSysDictService;

    @Autowired
    private CacheManager cacheManager;

    private String dict;


    @Override
    public Page pageDictItem(Page page, SysDictItem dictItem) {
        LambdaQueryWrapper<SysDictItem> queryWrapper = Wrappers.<SysDictItem>query().lambda()
                .orderByAsc(SysDictItem::getSort).orderByDesc(SysDictItem::getCreateTime);
        if (dictItem.getDictId() != null) {
            queryWrapper.eq(SysDictItem::getDictId, dictItem.getDictId());
        }
        if (dictItem.getState() != null) {
            queryWrapper.eq(SysDictItem::getState, dictItem.getState());
        }
        if (StringUtils.isNotBlank(dictItem.getItemName())) {
            queryWrapper.like(SysDictItem::getItemName, dictItem.getItemName());
        }
        Page data = this.page(page, queryWrapper);
        List<SysDictItem> dictItemList = data.getRecords();
        List<SysDictItemVO> dictItemVOList = dictItemList.stream().map(x -> {
            SysDictItemVO sysDictItemVO = new SysDictItemVO();
            BeanUtils.copyProperties(x, sysDictItemVO);
            SysDict sysDict = iSysDictService.getById(x.getDictId());
            sysDictItemVO.setDictCode(sysDict.getDictCode())
                    .setDictName(sysDict.getDictName());
            return sysDictItemVO;
        }).collect(Collectors.toList());
        data.setRecords(dictItemVOList);
        return data;
    }

    @Override
    public SysDictItemVO findDictItemCode(String code, String itemCode) {
        SysDictItemVO itemVO = new SysDictItemVO();
        SysDict sysDict = iSysDictService.getOne(Wrappers.<SysDict>query().lambda()
                .eq(SysDict::getDictCode, code));
        if (ObjectUtils.isEmpty(sysDict)) {
            return itemVO;
        }
        SysDictItem dictItem = this.getOne(Wrappers.<SysDictItem>query().lambda()
                .eq(SysDictItem::getDictId, sysDict.getId())
                .eq(SysDictItem::getItemCode, itemCode));
        if (ObjectUtils.isEmpty(dictItem)) {
            return itemVO;
        }
        BeanUtils.copyProperties(dictItem, itemVO);
        itemVO.setDictName(sysDict.getDictName()).setDictCode(sysDict.getDictCode());
        return itemVO;
    }

    @Override
    public boolean checkDictUnique(SysDictItem dictItem) {
        SysDictItem sysDictItem = this.getOne(Wrappers.<SysDictItem>query().lambda()
                .eq(SysDictItem::getDictId, dictItem.getDictId())
                .eq(SysDictItem::getItemCode, dictItem.getItemCode()));
        return (sysDictItem != null && sysDictItem.getId() == null) ||
                (sysDictItem != null && !sysDictItem.getId().equals(dictItem.getId()));
    }

    @Cacheable(value = "sys-dict",key="#code")
    @Override
    public List<SysDictItem> findDictItemCode(String code) {
        SysDict dict = iSysDictService.getOne(Wrappers.<SysDict>query().lambda()
                .eq(SysDict::getDictCode, code));
        if (dict == null) {
            throw new CustomException("字典组不存在");
        }
        return this.list(Wrappers.<SysDictItem>query().lambda()
                .eq(SysDictItem::getDictId, dict.getId()));
    }

    @Override
    public boolean save(SysDictItem entity) {
        SysDict dict = iSysDictService.getById(entity.getDictId());
        Cache cache = cacheManager.getCache(RedisConstant.SYS_DICT);
        if(cache != null){
            cache.evictIfPresent(dict.getDictCode());
        }
        return super.save(entity);
    }

    @Override
    public boolean updateById(SysDictItem entity) {
        SysDict dict = iSysDictService.getById(entity.getDictId());
        Cache cache = cacheManager.getCache(RedisConstant.SYS_DICT);
        if(cache != null){
            cache.evictIfPresent(dict.getDictCode());
        }
        return super.updateById(entity);
    }
}
